package com.my.hotel.controller.serverPage;

import com.my.hotel.entity.serverPage.JdUser;
import com.my.hotel.service.JdUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * author:lwj
 * date: 2022/9/3 15:20
 */
@Controller
@RequestMapping(value = "/jd",produces = "text/html;charset=utf-8")
@Slf4j
public class JDLoginController {
@Autowired
private JdUserService service;
@Autowired
private UtilsController utilsController;
    @RequestMapping("/register")
    public String jdRegister(MultipartFile imgFile,String varifyCode, JdUser user, HttpSession session, Model model, HttpServletRequest req){
        Object validateCode = session.getAttribute("validateCode");

        if(validateCode == null)
            return utilsController.forwardToLogin("login",0,req);
        String strDirPath = req.getSession().getServletContext().getRealPath("/img");
        File file = new File(strDirPath);
        if(!file.exists()){
            file.mkdirs();
        }
        if(!imgFile.isEmpty()){

            try {
                String originalFilename = imgFile.getOriginalFilename();
                String s = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
                imgFile.transferTo(new File(strDirPath,s));
                user.setVia("../../../img/"+s);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println(strDirPath);
//        D:\dev\java\workspace\administration-hotel-system\target\hotel-1.0-SNAPSHOT\img
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date date1 = new Date();
        try {
            String format = sdf.format(date1);
            user.setCreate_time(format);
            user.setStatus(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        service.register(user,session,varifyCode,req);

      //todo  这里应该跳转到index 页，现在是假跳转
        return  utilsController.forwardToLogin(0,5,model);
    }


    @RequestMapping("/login")
    public String jdLogin(String dl_username, String dl_password, HttpSession session, Model model, HttpServletRequest req){
        String trim1 = dl_username.trim();
        String trim2 = dl_password.trim();
        boolean flag = service.isLogin(trim1,trim2,session);
        if(flag){
             return  utilsController.forwardToLogin(0,5,model);

        }else {
        //登陆失败 utils/forwardToLogin/login
            return utilsController.forwardToLogin("login",0,req);

        }
    }

    //用户名验重
    @RequestMapping("/userRepeat")
    @ResponseBody
    public ResponseEntity userIsRepeat(@RequestParam String username){
        ResponseEntity entity = null;
        int countByUsername = service.getCountByUsername(username);
        if(countByUsername==0){
            entity = ResponseEntity.status(HttpStatus.OK).body("ok");
        }else {
            entity = ResponseEntity.status(HttpStatus.OK).body("warning");
        }
        return entity;
    }


    @RequestMapping("/logout")
    public String logout(HttpServletRequest req){
        req.getSession().removeAttribute("userInfo");
        return utilsController.forwardToLogin("login",0,req);
    }

    @RequestMapping("/cklogin")
    public String cklogin(String username,Model model){
        JdUser jdUser= service.cklogin(username);
        if(jdUser==null){
            return null;
        }else{
            model.addAttribute("ljj","用户名已拥有");
            return null;
        }
    }
}
